/**********************************************************************************************/
/* FBL_CreateFile.cpp 	                                                   					  */
/*                                                                       					  */
/* Copyright Paradigma, 1993-2006															  */
/* All Rights Reserved                                                   					  */
/**********************************************************************************************/

#include <VShared/FBL/publ/Headers/StdAfx.h>

#include <VShared/FBL/publ/File_OS/FBL_File_OS.h>
#include <VShared/FBL/publ/Location/FBL_Location_Disk_FSSpec.h>
#include <VShared/FBL/publ/Location/FBL_Location_FSRef.h>
#include <VShared/FBL/publ/Util_classes/FBL_ToUChar.h>
#include <VShared/FBL/publ/Util_Strings/FBL_ustdio.h>


/**********************************************************************************************/
FBL_Begin_Namespace


/**********************************************************************************************/
I_File* CreateFile( const char* inPath )
{
	File_OS* pFile = new File_OS();
	
	I_Location_Ptr pLoc = CreateDiskLocation( inPath );
	pFile->put_Location( pLoc );
	
	return pFile;
}


/**********************************************************************************************/
I_File* CreateFile( const UChar* inPath )
{
	File_OS* pFile = new File_OS();
	
	I_Location_Ptr pLoc = CreateDiskLocation( inPath );
	pFile->put_Location( pLoc );
	
	return pFile;
}


/**********************************************************************************************/
I_File* CreateFile( I_Location_Ptr inLoc )
{
	File_OS* pFile = new File_OS();
	
	pFile->put_Location( inLoc );
	
	return pFile;
}


#pragma mark -


/**********************************************************************************************/
// Create folder on the disk.
//
I_Location_Ptr CreateFolder( I_Location_Ptr inParentLoc,  const char* inFolderName )
{
	// Location to be returned.
	I_Location_Ptr pLoc;

#if FBL_WIN
	pLoc = inParentLoc->get_ChildLocation( inFolderName );

	CreateDirectoryW( pLoc->get_Path().c_str(), NULL );
/*
	if( CreateDirectoryW( pLoc->get_Path().c_str(), NULL ) == false )
	{
		FBL_Throw( xOSFileError( 
						ERR_CREATE_FOLDER, 
						pLoc->get_Path().c_str(),
						ToUCharPtr(GetLastError()).c_str() ) );
	}
*/
	
#elif FBL_MAC
	Location_Disk_FSRef_Ptr pLocationRef = fbl_dynamic_cast<Location_Disk_FSRef>(inParentLoc);
	if( !pLocationRef )
	{
		Location_Disk_FSSpec_Ptr pLocationSpec = fbl_dynamic_cast<Location_Disk_FSSpec>(inParentLoc);
		
		pLocationRef = new Location_Disk_FSRef();
		pLocationRef->put_FSSpec( pLocationSpec->get_FSSpec() );
	}

	FSRef parentRef;
	OSStatus status = pLocationRef->get_ObjectFSRef( &parentRef );
	FBL_CHECK( status == 0 );				

	String newFolderName( inFolderName );	

	OSErr err = FSCreateDirectoryUnicode( 
					&parentRef, 
					(ulong) newFolderName.length(), 
					(UniChar*) newFolderName.c_str(),
					kFSCatInfoNone, NULL, NULL, NULL, NULL );					
	FBL_CHECK( err == 0 );		

	// For MAC OS X its important to create ChildLocation AFTER 
	// we have create disk folder, becuse then we get correct flag mObjectExists.
	pLoc = inParentLoc->get_ChildLocation( inFolderName );
	
#else // FBL_WIN
	
	#pragma error "Define CreateFolder() method for your platfrom."
			
#endif // FBL_WIN


	return pLoc;
}	


/**********************************************************************************************/
I_Location_Ptr CreateFolder( I_Location_Ptr inParentLoc,  const UChar* inFolderName )
{
	// Location to be returned.
	I_Location_Ptr pLoc = inParentLoc->get_ChildLocation( inFolderName );

	// Create folder on the disk.
	//
#if FBL_WIN
	CreateDirectoryW( pLoc->get_Path().c_str(), NULL );
/*
	if( CreateDirectoryW( pLoc->get_Path().c_str(), NULL ) == false )
	{
		FBL_Throw( xOSFileError( 
						ERR_CREATE_FOLDER, 
						pLoc->get_Path().c_str(),
						ToUCharPtr(GetLastError()).c_str() ) );
	}
*/
#elif FBL_MAC

	Location_Disk_FSRef_Ptr pLocationRef = fbl_dynamic_cast<Location_Disk_FSRef>(inParentLoc);
	if( !pLocationRef )
	{
		Location_Disk_FSSpec_Ptr pLocationSpec = fbl_dynamic_cast<Location_Disk_FSSpec>(inParentLoc);
		
		pLocationRef = new Location_Disk_FSRef();
		pLocationRef->put_FSSpec( pLocationSpec->get_FSSpec() );
	}

	FSRef parentRef;
	OSStatus status = pLocationRef->get_ObjectFSRef( &parentRef );
	FBL_CHECK( status == 0 );				

	OSErr err = FSCreateDirectoryUnicode( 
					&parentRef, 
					(ulong) vu_strlen( inFolderName ),
					(UniChar*) inFolderName,
					kFSCatInfoNone, NULL, NULL, NULL, NULL );				
	FBL_CHECK( err == 0 );		


#else // FBL_WIN
	
	#pragma error "Define CreateFolder() method for your platfrom."
			
#endif // FBL_WIN

	return pLoc;
}	


/**********************************************************************************************/
FBL_End_Namespace
